/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.spring.manager.impl;

import java.util.Collections;

import com.genia.toolbox.security.bean.Permission;
import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.User;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.AuthorizationManager;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.GroupRoleManager;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.RoleDAO;

/**
 * implementation of {@link AuthorizationManager}.
 */
public class AuthorizationManagerImpl
    implements AuthorizationManager
{

  /**
   * the {@link GroupRoleManager} to use.
   */
  private GroupRoleManager groupRoleManager;

  /**
   * the {@link RoleDAO} to use.
   */
  private RoleDAO roleDAO;



  /**
   * returns whether the given user has the given permission.
   * 
   * @param user
   *          the user to check for
   * @param permission
   *          the permission to check for
   * @return whether the given user has the given permission
   * @throws TechnicalException
   *           when an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.AuthorizationManager#isAuthorized(com.genia.toolbox.projects.toolbox_basics_project.bean.model.User,
   *      com.genia.toolbox.projects.toolbox_basics_project.bean.PermissionDescription)
   */
  public boolean isAuthorized(User user, Permission permission)
      throws TechnicalException
  {
    if (user == null) {
      return false;
    }
    return !Collections.disjoint(getGroupRoleManager().getRoles(user.getGroups()), getRoleDAO().getRolesForPermission(permission));
  }



  /**
   * getter for the groupRoleManager property.
   * 
   * @return the groupRoleManager
   */
  public GroupRoleManager getGroupRoleManager()
  {
    return groupRoleManager;
  }



  /**
   * setter for the groupRoleManager property.
   * 
   * @param groupRoleManager
   *          the groupRoleManager to set
   */
  public void setGroupRoleManager(GroupRoleManager groupRoleManager)
  {
    this.groupRoleManager = groupRoleManager;
  }



  /**
   * getter for the roleDAO property.
   * 
   * @return the roleDAO
   */
  public RoleDAO getRoleDAO()
  {
    return roleDAO;
  }



  /**
   * setter for the roleDAO property.
   * 
   * @param roleDAO
   *          the roleDAO to set
   */
  public void setRoleDAO(RoleDAO roleDAO)
  {
    this.roleDAO = roleDAO;
  }

}
